<template>
	<view>
		<st-mc-framework navTitle="古越电源注塑MES" @changeLevel="changeLevel">
			<template #content>
				<view class="content" style="height: 100%;">
					<view class="contentBox">
						<view class="container">
							<uni-forms ref="baseForm" :modelValue="baseFormData">
								<view class="form-item-row">

									<uni-forms-item label="派工单编号:" label-width="auto">
										<input v-model="baseFormData.PGBillNo" type="text" placeholder="自动带出派工单编号"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="开工编号:" label-width="auto">
										<input v-model="baseFormData.BillNO" type="text" placeholder="自动带出开工编号"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="开工日期:" label-width="auto">
										<input v-model="baseFormData.StartSingleDate" disabled type="text"
											placeholder="自动带出开工日期" />
										<text class="text" @click="show=true"></text>

									</uni-forms-item>
									<uni-forms-item label="产品名称:" label-width="auto" disabled>
										<input v-model="baseFormData.ProductName" type="text" placeholder="自动带出产品名称"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="开工数量:" label-width="auto" required>
										<input v-model="baseFormData.StartQty" type="text" placeholder="开工数量" />
									</uni-forms-item>
									<!-- 下拉 -->
									<uni-forms-item label="线体:" label-width="auto" required>
										<!-- 后期要改 设备名称 -->
										<!-- <uni-forms-item label="设备名称:" label-width="auto" required> -->
										<view class="uni_forms_item_view" @click="openBaseScreen('机台')">

											<text v-if="!baseFormData.EquipmentName"
												class="u_text">{{FormData.EquipmentName}}</text>
											<text v-else>{{baseFormData.EquipmentName}}</text>
										</view>
									</uni-forms-item>

									<uni-forms-item label="工序名称:" label-width="auto" required>
										<input v-model="baseFormData.ProcedureName" type="text" placeholder="请选择工序名称"
											disabled />
										<text class="text" @click="addProcedurname('手动')"></text>
									</uni-forms-item>
									<uni-forms-item label="生产车间:" label-width="auto">
										<input v-model="baseFormData.FWorkshopName" type="text" placeholder="自动带出生产车间"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="是否开工:" label-width="auto">
										<input v-model="baseFormData.IsStart?'已开工':'未开工'" type="text"
											placeholder="自动带出是否开工" disabled />
									</uni-forms-item>
									<uni-forms-item label="检验员:" label-width="auto" required>
										<input v-model="baseFormData.QCEmplName" type="text" disabled
											placeholder="自动带出检验员" />
										<!-- <text class="text" @click="openBaseScreen('人员')"></text> -->

										<!-- <view class="uni_forms_item_view" @click="openBaseScreen('人员')">
											<text v-if="!baseFormData.QCEmplName"
												class="u_text">{{FormData.QCEmplName}}</text>
											<text v-else>{{baseFormData.QCEmplName}}</text>
										</view> -->
									</uni-forms-item>
									<uni-forms-item label="开工人:" label-width="auto">
										<input v-model="baseFormData.StartEmplName" type="text" disabled
											placeholder="自动带出开工人" />
									</uni-forms-item>
									<uni-forms-item label="模具:" label-width="auto" required>
										<view class="uni_forms_item_view" @click="openBaseScreen('注塑模具')">
											<text v-if="!baseFormData.MoludName"
												class="u_text">{{FormData.MoludName}}</text>
											<text v-else>{{baseFormData.MoludName}}</text>
										</view>
									</uni-forms-item>
								</view>
							</uni-forms>



						</view>

					</view>

					<view class="DetailsbottomBtns">

						<button class="button1" block @click="submit">
							{{changeButton}}
						</button>
						<button class="button2" block @click="continueClick" v-show='ButtonShowTitle'>
							<!-- <button class="button2" v-show='ButtonShowTitle' block @click="continueClick"> -->
							继续开工
						</button>
					</view>
					<u-datetime-picker :show="show" v-model="value1" mode="date" @cancel='show=false'
						@confirm='pickerConfirm'></u-datetime-picker>

					<u-action-sheet :actions="showNameList" :show="showName" @select="addName"
						@close="showName=false"></u-action-sheet>

					<u-action-sheet :actions="ProcedurnameList" :show="ProcedurnameShow" @select="ProcedurnameClick"
						@close="ProcedurnameShow=false"></u-action-sheet>

					<u-toast ref="uToast"></u-toast>

					<u-popup :show="popup_logout" mode="center" :closeOnClickOverlay="false" :round="4">
						<view class="popup-content">
							<view class="popup-content_close x-c">
								<uni-icons type="closeempty" color="#999" size="20"
									@click='popup_logout = false'></uni-icons>
							</view>
							<view class="x-c title">
								<text>{{popuptitle}}</text>
							</view>
							<view class="contentView">
								<text>{{popupcontent}}</text>
							</view>
							<view class="btnBox x-ac">

								<u-button type="primary" text="确定" @click="confirmClick"></u-button>
							</view>
						</view>
					</u-popup>


				</view>
			</template>

		</st-mc-framework>
	</view>
</template>
<script>
	import {
		getTimeStamp,
		getFormatDateTime,
		getBefore7DateTime,
		formatDateTime,
		navigateMixin,
		baseAddModuleData_select,
		getchangeLevel
	} from '@/st_mes_zs/common/base.js';
	
	export default {
		mixins: [navigateMixin, baseAddModuleData_select],
		data() {
			return {
				popuptitle: '',
				popupcontent: '',
				popup_logout: false,

				FormData: {
					MoludName: '请选择模具名称',
					EquipmentName: '请选择线体',
					QCEmplName: '请选择人员'
				},

				baseFormData: {
					BillNO: '',
					StartSingleDate: '',
					ProductName: '',
					StartQty: '',
					EquipmentName: '',
					EquipmentCode: '',
					EquipmentID: 0,
					MoludName: '',
					MoludID: null,
					FlowOrder: '',
					ProcedureName: '',
					FWorkshopName: '',
					IsStart: '',
					StartEmplID: '',
					StartEmplName: '',
					QCEmplID: '',
					QCEmplName: ''
				},
				sexs: [{
					text: '合格',
					value: 0
				}, {
					text: '不合格',
					value: 1
				}],
				TimeName: '',
				show: false,
				value1: Number(new Date()),
				showName: false,
				showNameList: [],
				changeName: '',
				changeButton: '',
				Equipmentshow: false,
				getEquipmentList: [],
				ProcedurnameShow: false,
				ProcedurnameList: [],
				ButtonShowTitle: false
			};
		},
		mounted() {},
		onLoad(onload) {
			var that = this
			uni.hideKeyboard(); //隐藏软键盘  
			var item = JSON.parse(onload.item)
			console.log(this.$store.state.UserInfo);
			this.$store.state.GuyueModule.MesBasicInfo.Status.ClockStatusTitle = '其他'

			if (item.StartSingleID) {
				this.queryList(item.StartSingleID)
			} else {

				this.baseFormData = item
				this.changebuttons(item.StartSingleID)

				this.baseFormData.StartQty = this.baseFormData.StartQty ? this.baseFormData.StartQty : this.baseFormData
					.FPlanQty
				this.baseFormData.MoludID = this.baseFormData.MoludID == null ? 0 : this.baseFormData.MoludID
				this.baseFormData.StartEmplName = this.baseFormData.StartEmplName ? this.baseFormData.StartEmplName : this
					.$store.state.UserInfo.EmplName
				this.baseFormData.StartEmplID = this.baseFormData.StartEmplID ? this.baseFormData.StartEmplID : this.$store
					.state.UserInfo.EmplID

				this.baseFormData.QCEmplName = this.baseFormData.StartEmplName ? this.baseFormData.StartEmplName : this
					.$store.state.UserInfo.EmplName
				this.baseFormData.QCEmplID = this.baseFormData.StartEmplID ? this.baseFormData.StartEmplID : this.$store
					.state.UserInfo.EmplID

				this.baseFormData.StartSingleDate = this.baseFormData.StartSingleDate ? this.baseFormData.StartSingleDate :
					getFormatDateTime(new Date())

				that.baseFormData.EquipmentName = that
					.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentName
				that.baseFormData.EquipmentID = that
					.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentID
			}
		},
		onShow() {
			this.getValue
		},
		onBackPress(options) { // 监听页面返回
			if (options.from == 'navigateBack') { // 通过点击路由返回
				return false;
			} else if (options.from == 'backbutton') { // 通过外置返回
				return true;
			}
		},
		methods: {

			confirmClick() {
				this.popup_logout = false
			},
			addShowEquipment() {
				if (this.baseFormData.EquipmentID) {

					this.baseFormData.EquipmentName = this.baseFormData.EquipmentName
					this.baseFormData.EquipmentID = this.baseFormData.EquipmentID
				} else if (this.baseFormData.JobEquipmentID) {

					this.baseFormData.EquipmentName = this.baseFormData.JobEquipmentName
					this.baseFormData.EquipmentID = this.baseFormData.JobEquipmentID
				} else {

					this.baseFormData.EquipmentName = this
						.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentName
					this.baseFormData.EquipmentID = this
						.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentID
				}

			},


			getValue(e) {
				var that = this;
				console.log(e);
				if (e != '返回') {
					if (e.type == '人员') {
						that.baseFormData.QCEmplName = e.UserName
						that.baseFormData.QCEmplID = e.UserID

					} else if (e.type == '机台') {
						that.baseFormData.EquipmentID = e.EquipmentID
						that.baseFormData.EquipmentName = e.EquipmentName

					} else if (e.type == '注塑模具') {
						that.baseFormData.MoludID = e.MoludID
						that.baseFormData.MoludName = e.MoludName

					}


					that.$forceUpdate()
				}
			},


			openBaseScreen(str) {
				uni.hideKeyboard(); //隐藏软键盘
				var that = this;
				var obj;
				if (str == '机台') {
					obj = {
						cmd: 'getEquipment',
						type: str,
						serverSearch: {
							isPaging: false, //是否分页
							// pageIndexStr: 'PageIndex', //分页页码字段，应包含在param中		
							searchShow: false, //是否查询
							searchKey: 'searchKey', //搜索关键词，应包含在param中
							showText: 'EquipmentName', //查询结果显示的字段
						},
						param: {
							FWorkshopID: '1011',
						}
					}
				} else if (str == '人员') {
					obj = {
						cmd: 'getEmplByJobName',
						type: str,
						serverSearch: {
							isPaging: true, //是否分页
							pageIndexStr: 'PageIndex', //分页页码字段，应包含在param中		
							searchShow: false, //是否查询
							searchKey: 'searchKey', //搜索关键词，应包含在param中
							showText: 'UserName', //查询结果显示的字段
						},
						param: {
							JobName: '质量',
							PageIndex: 1,
							PageSize: 30
						}
					}


				} else if (str == '注塑模具') {


					obj = {
						cmd: 'getMoludByProduct',
						type: str,
						serverSearch: {
							isPaging: false, //是否分页
							pageIndexStr: 'PageIndex', //分页页码字段，应包含在param中		
							searchShow: false, //是否查询
							searchKey: 'searchKey', //搜索关键词，应包含在param中
							showText: 'MoludName', //查询结果显示的字段
						},
						param: {
							ProductID: that.baseFormData.ProductID,
							FWorkshopID: that.baseFormData.FWorkshopID,

						}
					}

				}

				uni.navigateTo({
					url: `../common/baseScreen?obj=${JSON.stringify(obj)}`,
					animationType: 'slide-in-right',
					animationDuration: 300
				});
			},
			continueClick() {
				var that = this
				var formdata = that.baseFormData
				formdata.StartSingleID = 0
				formdata.BillNO = ''
				formdata.IsStart = 0
				this.$forceUpdate()
				this.changebuttons(formdata.StartSingleID)
				that.addProcedurname('自动')
			},

			// 工序 
			ProcedurnameClick(e) {
				var that = this

				that.baseFormData = {
					...that.baseFormData,
					...e
				}
				if (e.EquipmentID) {
					that.baseFormData.EquipmentID = e.EquipmentID
					that.baseFormData.EquipmentName = e.EquipmentName
				} else {
					that.baseFormData.EquipmentName = that
						.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentName
					that.baseFormData.EquipmentID = that
						.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentID
				}
				that.baseFormData.MoludID = that.baseFormData.MoludID == null ? 0 : that.baseFormData.MoludID
				that.baseFormData.IsStart = ''
				console.log(that.baseFormData);
				that.$forceUpdate()
				that.ProcedurnameShow = false

			},
			addProcedurname(str) {
				var that = this;
				var obj = {
					EquipmentID: that.baseFormData.EquipmentID,
					IsStart: 0,
					JobOrderID: this.baseFormData.JobOrderID,
				}
				that.Request.request('getJobOrderPROC', obj, function(ret) {
					console.log(ret);
					if (ret.status == 200) {

						if (str == '手动') {
							ret.data.ds.map(item => item.name = item.ProcedureName)
							that.ProcedurnameList = ret.data.ds
							that.ProcedurnameShow = true
						} else {
							if (ret.data.ds.length > 0) {
								console.log(that.baseFormData);
								var e = ret.data.ds[0]
								console.log(e);
								that.baseFormData = {
									...that.baseFormData,
									...e
								}
								console.log(that.baseFormData);
								if (e.EquipmentID) {
									that.baseFormData.EquipmentID = e.EquipmentID
									that.baseFormData.EquipmentName = e.EquipmentName
								} else {
									that.baseFormData.EquipmentName = that
										.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentName
									that.baseFormData.EquipmentID = that
										.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentID
								}
								that.$forceUpdate()
							}


						}

					}
				})
			},


			addName(e) {
				var that = this
				console.log(e);
				that.baseFormData.QCEmplID = e.EmplID
				that.baseFormData.QCEmplName = e.EmplName
				that.showName = false
				this.$forceUpdate()
			},

			changebuttons(ID) {
				var that = this;
				if (ID == 0) {
					that.changeButton = '开工'
				} else {
					that.changeButton = '保存'
					that.Request.request('IfShowKGButton', {
						JobOrderID: that.baseFormData.JobOrderID
					}, (res) => {
						console.log(res.data.ds);
						if (res.status == 200) {
							if (res.data.ds[0].CC > 0) {
								that.ButtonShowTitle = true

							} else {
								that.ButtonShowTitle = false
							}
						}
					})

				}
			},

			// 获取人员
			openBase(str) {
				var that = this;
				var obj = {}
				var cmd
				if (str == '检验员') {
					that.changeName = str
					cmd = 'getEmplByJobName'
					obj.JobName = '质量'
					obj.PageIndex = 1
					obj.PageSize = 30
				}
				that.Request.request(cmd, obj, function(ret) {
					console.log(ret);
					if (ret.status == 200) {
						that.showName = true
						ret.data.ds.map(item => item.name = item.EmplName)
						that.showNameList = ret.data.ds
					}
				})
			},
			// 获取时间
			pickerConfirm(e) {
				var time = getFormatDateTime(e.value)
				this.baseFormData.StartSingleDate = time
				this.show = false
				this.$forceUpdate()
			},

			// 保存
			submit() {
				var that = this;
				var show = true
				var msg

				if (!that.baseFormData.QCEmplName) {
					msg = '请输入检验员'
					show = false
				} else if (that.baseFormData.StartQty == '' || that.baseFormData.StartQty == null) {
					msg = '请输入开工数量'
					show = false
				} else if (!that.baseFormData.EquipmentName) {
					msg = '请输入设备名称'
					show = false
				} else if (!that.baseFormData.ProcedureName) {
					msg = '请输入工序'
					show = false
				} else if (!that.baseFormData.MoludID) {
					msg = '请选择模具'
					show = false
				}
				console.log(this.baseFormData);
				if (show) {
					// if (that.baseFormData.MoludID == null || that.baseFormData.MoludID == '') {
					// 	that.baseFormData.MoludID = 0
					// }
					that.Request.request('saveStartOrder', that.baseFormData, function(ret) {
						console.log(ret);
						if (ret.status == 200) {

							if (ret.data.ds[0].status == 500) {


								that.popuptitle = '报错'
								that.popupcontent = ret.data.ds[0].msg
								that.popup_logout = true
							} else {
								that.$refs.uToast.show({
									title: '保存',
									type: 'success',
									message: '保存成功'
								})
								setTimeout(() => {
									that.queryList(ret.data.ds[0].StartSingleID)
								}, 1000)
							}


						}
					})

				} else {

					that.popuptitle = '报错'
					that.popupcontent = msg
					that.popup_logout = true

				}

			},
			queryList(id) {
				var that = this;
				that.Request.request('getStartOrderDetail', {
					StartSingleID: id
				}, (ret) => {
					console.log(ret.data.ds);
					if (ret.status == 200) {
						that.baseFormData = ret.data.ds[0]
						that.baseFormData.StartSingleDate = that.baseFormData.StartSingleDate ? that.baseFormData
							.StartSingleDate.split('T')[0] : ''
						that.baseFormData.MoludID = that.baseFormData.MoludID == null ? 0 : that.baseFormData
							.MoludID


						that.$forceUpdate()
						that.changebuttons(that.baseFormData.StartSingleID)
					}
				})
			},
			// 跳转tag
			changeLevel(data, row) {
				getchangeLevel(data)
				uni.$emit('refreshList');
			},
		}
	}
</script>

<style lang="scss" >
	@import "../css/index.css";

	.form-item-row {
		display: flex;
		flex-wrap: wrap;
	}
</style>